package dhk;

import static org.junit.Assert.*;
import java.awt.geom.Point2D;
import org.junit.Test;

/**
 * Junit TestTrackingUtiltiy.
 * 
 * @author Dean Kim
 * 
 */
public class TestTrackingUtility {

  /**
   * Test computing a position from a given source position and a bearing/range.
   */
  @Test
  public void testComputeTargetPositFromBearingRange() {

    Point2D testStartPoint2D = new Point2D.Double();
    Point2D testResult2D = new Point2D.Double();
    Point2D battleCenter2D = new Point2D.Double();

    battleCenter2D.setLocation(400, 600);
    testStartPoint2D.setLocation(0, 0);

    testResult2D = TrackingUtility.computeTargetPositFromBearingRange(testStartPoint2D, 90, 10);
    assertEquals("start 0,0 brg 90 rng 10: Xval", testResult2D.getX(), 10, 0.0001);
    assertEquals("start 0,0 brg 90 rng 10: Yval", testResult2D.getY(), 0, 0.0001);

    testResult2D = TrackingUtility.computeTargetPositFromBearingRange(testStartPoint2D, 0, 10);
    assertEquals("start 0,0 brg 0 rng 10: Xval", testResult2D.getX(), 0, 0.0001);
    assertEquals("start 0,0 brg 0 rng 10: Yval", testResult2D.getY(), 10, 0.0001);

    testStartPoint2D.setLocation(10, 10);

    testResult2D = TrackingUtility.computeTargetPositFromBearingRange(testStartPoint2D, 270, 10);
    assertEquals("start 10,10 brg 270 rng 10): Xval", testResult2D.getX(), 0, 000.1);
    assertEquals("start 10,10 brg 270 rng 10): Yval", testResult2D.getY(), 10, 000.1);

    testResult2D = TrackingUtility.computeTargetPositFromBearingRange(testStartPoint2D, 225,
        14.1421356);
    assertEquals("start 10,10 brg 225 rng 14.1421356): Xval", testResult2D.getX(), 0, 000.1);
    assertEquals("start 10,10 brg 225 rng 14.1421356): Yval", testResult2D.getY(), 0, 000.1);

    testResult2D = TrackingUtility.computeTargetPositFromBearingRange(testStartPoint2D, 45,
        14.1421356);
    assertEquals("start 10,10 brg 45 rng 14.1421356): Xval", testResult2D.getX(), 20, 000.1);
    assertEquals("start 10,10 brg 45 rng 14.1421356): Yval", testResult2D.getY(), 20, 000.1);

  }

  /**
   * Test computing a firing bearing from a given source position to a target position.
   */
  @Test
  public void testComputeFiringTrueBearing() {

    Point2D source = new Point2D.Double();
    Point2D target = new Point2D.Double();

    source.setLocation(10, 10);
    target.setLocation(10, 100);

    assertEquals("Source 10,10 target 10,100", TrackingUtility.computeFiringTrueBearing(source,
        target), 0, 0.0001);
    target.setLocation(100, 10);
    assertEquals("Source 10,10 target 100,10", TrackingUtility.computeFiringTrueBearing(source,
        target), 90, 0.0001);
    target.setLocation(10, 0);
    assertEquals("Source 10,10 target 10,0", TrackingUtility.computeFiringTrueBearing(source,
        target), 180, 0.0001);
    target.setLocation(0, 10);
    assertEquals("Source 10,10 target 0,10", TrackingUtility.computeFiringTrueBearing(source,
        target), 270, 0.0001);
    target.setLocation(20, 20);
    assertEquals("Source 10,10 target 20,20", TrackingUtility.computeFiringTrueBearing(source,
        target), 45, 0.0001);
    target.setLocation(20, 0);
    assertEquals("Source 10,10 target 0,20", TrackingUtility.computeFiringTrueBearing(source,
        target), 135, 0.0001);
    target.setLocation(0, 0);
    assertEquals("Source 10,10 target 0,20", TrackingUtility.computeFiringTrueBearing(source,
        target), 225, 0.0001);
    target.setLocation(0, 20);
    assertEquals("Source 10,10 target 0,20", TrackingUtility.computeFiringTrueBearing(source,
        target), 315, 0.0001);
  }

}
